package com.fjec.market.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjec.common.annotation.Json;
import com.fjec.common.protocol.JsonResult;
import com.fjec.market.schedule.handler.RequestPhyDivideHandler;
import com.fjec.market.schedule.handler.YsDeliveryHandler;
import com.fjec.market.schedule.handler.YsSignHandler;
import com.fjec.market.service.IBizPhysicalOrderService;
import com.fjec.market.view.OrderItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 041.实物订单表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2020-09-21
 */
@RestController
@RequestMapping("/")
public class BizPhysicalOrderController {

    @Resource
    private IBizPhysicalOrderService service;

    @Autowired
    private RequestPhyDivideHandler requestPhyDivideHandler;

    /**
     * 提交订单
     *
     * @param addressName  联系人
     * @param addressPhone 联系电话
     * @param address      地址
     * @param totalPrice   总金额
     * @param orderType    订单类型 1=水果零售订单 2=水果批量采购订单
     * @param productList
     * @return
     */
    @PostMapping("/y/phy/order/save")
    public JsonResult saveOrder(String addressName,
                                String addressPhone,
                                String address,
                                Integer totalPrice,
                                Integer orderType,
                                @Json List<OrderItem> productList) {
        return service.saveOrder(addressName, addressPhone, address, totalPrice, orderType, productList);
    }

    /**
     * 订单查询
     *
     * @param page
     * @param status 1=全部
     *               2=待付款
     *               3=待发货
     *               4=待收货
     *               5=待评价
     *               6=投诉
     * @return
     */
    @GetMapping("/y/phy/order/list")
    public JsonResult listForOrderStatus(Page page, Integer status) {
        return service.listForOrderStatus(page, status);
    }

    /**
     * 出售订单查询
     *
     * @param page
     * @param status 1=全部
     *               2=待付款
     *               3=待发货
     *               4=待收货
     *               5=待评价
     *               6=投诉
     * @return
     */
    @GetMapping("/y/phy/sale/order/list")
    public JsonResult getSaleOrderList(Page page, Integer status) {
        return service.getSaleOrderList(page, status);
    }

    /**
     * 订单详情
     *
     * @param id
     * @return
     */
    @GetMapping("/y/phy/order/detail")
    public JsonResult subOrderDetail(Long id) {
        return service.subOrderDetail(id);
    }

    /**
     * 子订单发货
     *
     * @param id       子订单Id
     * @param deliType 发货类型 1=物流 2=快递 3=其他
     * @param deliId   快递Id
     * @param deliName 快递名称
     * @param orderNum 快递单号
     * @param person   物流联系人
     * @param phone    物流联系电话
     * @return
     */
    @PostMapping("/y/phy/order/sub/delivery")
    public JsonResult subOrderDelivery(Long id,
                                       Integer deliType,
                                       @RequestParam(required = false) Long deliId,
                                       @RequestParam(required = false) String deliName,
                                       @RequestParam(required = false) String orderNum,
                                       @RequestParam(required = false) String person,
                                       @RequestParam(required = false) String phone) {
        return service.subOrderDelivery(id, deliType, deliId, deliName, orderNum, person, phone);
    }

    /**
     * 货物签收
     *
     * @param id
     * @return
     */
    @PostMapping("/y/phy/order/sub/signed")
    public JsonResult subOrderSigned(Long id) {
        return service.subOrderSigned(id);
    }

    /**
     * 根据子订单Id获取相同主订单其他订单信息
     *
     * @param orderId
     * @param subOrderId
     * @return
     */
    @GetMapping("/y/phy/get/same/order")
    public JsonResult getSameSubOrderByOrderId(Long orderId, Long subOrderId) {
        return service.getSameSubOrderByOrderId(orderId, subOrderId);
    }


    @GetMapping("/phy/order/request/divide/test")
    public String testDivide() {
        requestPhyDivideHandler.handle(50, 0);
        return "success";
    }

    @Autowired
    private YsDeliveryHandler ysDeliveryHandler;
    @GetMapping("/phy/order/ys/deliver/test")
    public String ysDeliverTest() {
        String log = ysDeliveryHandler.handle(1);
        return log;
    }

    @Autowired
    private YsSignHandler ysSignHandler;
    @GetMapping("/phy/order/ys/sign/test")
    public String ysSignTest() {
        String log = ysSignHandler.handle(1);
        return log;
    }
}
